﻿using Microsoft.AspNetCore.Mvc;
using Simple.Services.System.Jobs;
using Microsoft.Extensions.DependencyInjection;
using System;
using Simple.Services.System;

[Route("[controller]")]
[ApiController]
public class TimesTimerController : ControllerBase
{
    private readonly TimesTaskService _timerTask;

    public TimesTimerController(TimesTaskService timerTask)
    {
        _timerTask = timerTask;
        _timerTask.Initialize(43200000, async () =>
        {
            using (var cancellationTokenSource = new CancellationTokenSource())
            {
                await _timerTask.UpdateTimesAsync(cancellationTokenSource.Token);
            }
        });
    }

    // GET: timer/start
    [HttpGet("start")]
    public IActionResult StartTimer()
    {
        try
        {
            _timerTask.Start();
            return Ok("Timer started.");
        }
        catch (Exception ex)
        {
            return StatusCode(500, $"Error starting timer: {ex.Message}");
        }
    }

    // GET: timer/stop
    [HttpGet("stop")]
    public IActionResult StopTimer()
    {
        try
        {
            _timerTask.Stop();
            return Ok("Timer stopped.");
        }
        catch (Exception ex)
        {
            return StatusCode(500, $"Error stopping timer: {ex.Message}");
        }
    }
}
